//
//  BHApplyUserInfoWriteView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/16.
//

import UIKit
@objc protocol BHApplyUserInfoWriteViewDelegate: NSObjectProtocol {
    func applyWriteUserInfoCall(model: BHApplyUserModel,index: Int)
}

class BHApplyUserInfoWriteView: UIView {
    
    var delegate: BHApplyUserInfoWriteViewDelegate?
    
    
    lazy var contentBackView: UIView = {
        let view = UIView.init()
        view.backgroundColor = .white
        return view
    }()
  
    lazy var tableView: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.register(UINib.init(nibName: "BHUserInfoWriteCell", bundle: nil), forCellReuseIdentifier: "BHUserInfoWriteCell")
        view.delegate = self
        view.dataSource = self
        view.bounces = false
        return view
    }()
    
    lazy var title_label: UILabel = {
        let label = UILabel.init()
        label.text = "报名信息"
        label.textColor = .color_333333
        label.font = .systemFont(ofSize: 16, weight: .medium)
        label.textAlignment = .center
        return label
    }()
    lazy var closeBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setImage(UIImage.init(named: "bh_com_vcClose_sign"), for: .normal)
        return btn
    }()
    
    lazy var confirmBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.backgroundColor = .color_34CE98
        btn.setTitle("确认", for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .bold)
        btn.setTitleColor(.white, for: .normal)
        return btn
    }()

    fileprivate var teamUserIndex: Int = 0
    fileprivate var tempModel: BHApplyUserModel = BHApplyUserModel.init()
    fileprivate  var enrollDic: [String:Any] = [:]
    
    
    func writeModelAndIndex(model: BHApplyUserModel, index: Int) {
        self.teamUserIndex = index
        self.enrollDic = model.textDic
        self.tempModel = model
        self.tableView.reloadData()
    }
    
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isHidden = true
        self.backgroundColor = UIColor.black.withAlphaComponent(0.4)
        self.contentBackView.frame = CGRect(x: 0, y: UIScreen.begainY, width: UIScreen.width, height: UIScreen.showHeight)
        self.title_label.frame = CGRect(x: (UIScreen.width-200)/2.0, y: 30, width: 200, height: 20)
        self.closeBtn.frame = CGRect(x: UIScreen.width-50, y: 20, width: 40, height: 40);
        self.confirmBtn.frame = CGRect(x: 16, y: UIScreen.showHeight-10-UIScreen.safeBottomHeight-40, width: UIScreen.width-32, height: 40)
        self.tableView.frame = CGRect(x: 0, y: 58, width: UIScreen.width, height: self.contentBackView.bh_height-58-10-UIScreen.safeBottomHeight-40)
        
        self.addSubview(self.contentBackView)
        self.contentBackView.addSubview(self.title_label)
        self.contentBackView.addSubview(self.closeBtn)
        self.contentBackView.addSubview(self.tableView)
        self.contentBackView.addSubview(self.confirmBtn)
        self.confirmBtn.addLayerCorner(20, 0, .white)
        
        self.contentBackView.transform = CGAffineTransform.init(translationX: 0, y: UIScreen.showHeight)
        
        self.closeBtn.addBtnAction(action: {[weak self] (_) in
            self?.enrollDic = [:]
            self?.endEditing(true)
            self?.hide()
        }, for: .touchUpInside)
        
        self.confirmBtn.addBtnAction(action: {[weak self] (_) in
            self?.endEditing(true)
    
            self?.checkTextInfo()
        }, for: .touchUpInside)
        
        self.contentBackView.addCorner(conrners: [.topLeft,.topRight], radius: 12)
    }

    
    func show()  {
        self.isHidden = false
        UIView.animate(withDuration: 0.25) {
            self.contentBackView.transform = CGAffineTransform.identity
        }
    }
    
    func hide() {
        UIView.animate(withDuration: 0.25) {
            self.contentBackView.transform = CGAffineTransform.init(translationX: 0, y: UIScreen.showHeight)
        } completion: { (finished) in
            self.isHidden = true
            
        }
    }
    
    private func checkTextInfo() {
        for (key,content) in self.enrollDic {
            if content is String {
                let val = content as! String
                if val.isEmpty == true {
                    BHToast.toast(text: "请输入\(key)")
                    return
                }
                if key.contains("手机") || key.contains("联系") {
                    if val.count != 11 || !val.hasPrefix("1"){
                        BHToast.toast(text: "请输入正确的\(key)")
                        return
                    }
                }
            }
        }
        
        self.tempModel.textDic = self.enrollDic
        self.delegate?.applyWriteUserInfoCall(model: self.tempModel, index: self.teamUserIndex)
        self.hide()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.endEditing(true)
    }
}

extension BHApplyUserInfoWriteView: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        return self.nameArray.count
        return self.enrollDic.keys.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
       return 53
    }
    
}

extension BHApplyUserInfoWriteView: UITableViewDelegate {
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BHUserInfoWriteCell", for: indexPath) as! BHUserInfoWriteCell
        let key = Array(self.enrollDic.keys)
        let value = Array(self.enrollDic.values)
        let keyText = key[indexPath.row] as String
        
        let valueText = value[indexPath.row] as! String
        cell.name_label.text = keyText
        cell.infoTextFiled.placeholder = "请输入\(keyText)"
        cell.infoTextFiled.text = "\(valueText)"
        if keyText.contains("手机") || keyText.contains("联系") {
            cell.infoTextFiled.keyboardType = .numberPad
        }else {
            cell.infoTextFiled.keyboardType = .default
        }
        cell.infoTextDidEndEditingBlock = { [weak self] text in
            self?.enrollDic[keyText] = text
        }
        return cell
    }
}
